package javaEE65_day03.StudentTeacherManger.dao;


import javaEE65_day03.StudentTeacherManger.domian.Teacher;

public class TeacherDao {
    //创建保存教师信息的容器
    private static Teacher[] teach = new Teacher[5];
    //添加教师信息
    public static boolean addTeacher(Teacher tea) {
        //判断当前容器是否为空
        int num = -1;
        for (int i = 0; i < teach.length; i++) {
            Teacher teac = teach[i];
            if (teac == null){
                num = i;
                break;
            }
        }
        //向业务类反馈添加结果
        if (num == -1){
            return false;
        }else {
            teach[num] = tea;
            return true;
        }

    }
    // 查看教师信息
    public static Teacher[] findAllTeacher() {

        return teach;
    }
    //删除教师信息
    public static boolean deleteTeacherById(String id) {
        //调用本类中getIndex方法查找该工号在容器中的索引
        int index = getIndex(id);
        //用null将该索引位置的元素覆盖
        if (index == -1){
            return false;
        }else {
            teach[index] = null;
        }
        //向业务类反馈结果
            return true;
    }
    //查找该用户输入的工号在容器中的索引
    private static int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < teach.length; i++) {
            Teacher teacher = teach[i];
            if (teacher.getJobNumber().equals(id)){
               index = i;
               break;
            }
        }
        return index;
    }
    //修改教师信息
    public static boolean updateTeacher(String id, Teacher teacher) {
        //调用本类中的getIndex方法找到用户输入的学号的索引
        int index = getIndex(id);
        //向业务类反馈结果
        if (index == -1){
            return false;
        }else {
            //将该索引位置, 使用teacher对象替换
            teach[index] = teacher;
        }
        //向业务类反馈结果
        return true;
    }
}
